﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Mapping.Attributes;
using NHibernate.Transform;
using NHibernate.Criterion;
using System.IO;
using System.Reflection;

namespace Service
{
    public class Util
    {
    }
    public class DataLayer
    {
        public static ISessionFactory SessionFactory = CreateSessionFactory();

        public static ISessionFactory CreateSessionFactory()
        {
            try
            {
                var config = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));

                HbmSerializer.Default.Validate = true;

                HbmSerializer ser = new HbmSerializer();

                var assembly = Assembly.GetAssembly(typeof(CCCustomer));
                HbmSerializer.Default.HbmAssembly = assembly.FullName;
                HbmSerializer.Default.HbmNamespace = typeof(CCCustomer).Namespace;
                var stream = HbmSerializer.Default.Serialize(assembly);
                config.AddInputStream(stream);

                return config.BuildSessionFactory();
            }
            catch (Exception ex){ }
            return null;
        }
        public static ISession OpenSession()
        {
            //return CreateSessionFactory().OpenSession();
            return SessionFactory.OpenSession();
        }
        public static void CloseSession(ISession CurrentSession)
        {
            if (CurrentSession != null)
            {
                CurrentSession.Close();
                CurrentSession.Dispose();
            }
        }

        public static int GetModuleID()
        {
            return 2;
        }
    }
}